import java.util.HashMap;
import java.util.HashSet;

public class L904 {
    public static void main(String[] args) {
        int[] fruits = {1, 0, 1, 4, 1, 4, 1, 2, 3};
        int totalFruit = new Solution().totalFruit(fruits);
        System.out.println(totalFruit);


    }


    static class Solution {
        public int totalFruit(int[] fruits) {
            int i = 0;
            int result = 0;
            HashMap<Integer, Integer> map = new HashMap<>();

            for (int j = 0; j < fruits.length; j++) {
                map.put(fruits[j], map.getOrDefault(fruits[j], 0) + 1);
                while (map.size() > 2) {
                    map.put(fruits[i], map.getOrDefault(fruits[i], 0) - 1);
                    if (map.get(fruits[i]) == 0) {
                        map.remove(fruits[i]);
                    }
                    i++;
                }
                result = Math.max(result, j - i + 1);
            }
            return result;
        }
    }
}
